package org.example.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.ibatis.annotations.*;
import org.example.pojo.Doctor.Department;
import org.example.pojo.Doctor.Doctor;
import org.example.pojo.Doctor.Hospital;
import org.example.pojo.Doctor.OutpatientClinic;

import java.util.List;

@Mapper
@DS("second")
public interface HospitalMapper {
    @Select("select * from hospitals")
    List<Hospital> getAllHospitals();
    //获取所有部门信息
    @Select("select d.*,h.hospital_name from departments d left join hospitals h on d.hospital_id = h.hospital_id")
    List<Department> getAllDepartment();
    //根据医院id获取部门信息
    @Select("select * from departments where hospital_id = #{hospitalId}")
    List<Department> getDepartmentsByHospitalId(Integer hospitalId);
    //获取所有门诊信息
    @Select("select o.* ,d.department_name from outpatientclinics o left join departments d on o.department_id = d.department_id")
    List<OutpatientClinic> getAllClinics();
    //根据部门id获取门诊信息
    @Select("select * from outpatientclinics where department_id = #{departmentId}")
    List<OutpatientClinic> getClinicsByDepartmentId(Integer departmentId);
    @Select("select * from doctors where clinic_id = #{clinicId}")
    List<Doctor> getDoctorsByClinicId(Integer clinicId);
    @Insert("insert into hospitals (hospital_id,hospital_name,address) values (#{hospitalId}, #{hospitalName}, #{address})")
    void addHospital(Hospital hospital);
    @Insert("insert into departments (department_id,department_name,contact_number,hospital_id) values (#{departmentId}, #{departmentName},#{contactNumber}, #{hospitalId})")
    void addDepartment(Department department);
    @Insert("insert into outpatientclinics (clinic_id,clinic_name,clinic_description,department_id) values (#{clinicId}, #{clinicName},#{clinicDescription}, #{departmentId})")
    void addClinic(OutpatientClinic clinic);
    @Insert("insert into doctors (username,name,gender,title,contact_number,clinic_id) values (#{username},#{name},#{gender},#{title},#{contactNumber}, #{clinicId})")
    void addDoctor(Doctor doctor);
    @Select("select * from doctors where username = #{username}")
    Doctor findByUserName(String username);
    @Select("select department_id from departments where hospital_id = #{hospitalId}")
    Integer getDepartmentIDByHospitalID(Integer hospitalId);
    @Select("select clinic_id from outpatientclinics where department_id = #{departmentId}")
    Integer getClinicIDByDepartmentID(Integer departmentId);
    @Select("select doctor_id from doctors where clinic_id = #{clinicId}")
    Integer getDoctorIDByClinicID(Integer clinicId);
    @Update("update departments set hospital_id = null where hospital_id = #{hospitalId}")
    void alterDepartmentsByHospitalID(Integer departmentId);
    @Update("update outpatientclinics set department_id = null where department_id = #{departmentId}")
    void alterClinicsByDepartmentID(Integer clinicId);
    @Update("update doctors set clinic_id = null where clinic_id = #{clinicId}")
    void alterDoctorsByClinicID(Integer doctorId);
    @Delete("delete from hospitals where hospital_id = #{hospitalId}")
    void deleteHospital(Integer hospitalId);
    @Delete("delete from departments where department_id = #{departmentId}")
    void deleteDepartment(Integer departmentId);
    @Delete("delete from outpatientclinics where clinic_id = #{clinicId}")
    void deleteClinic(Integer clinicId);
    @Delete("delete from doctors where doctor_id = #{doctorId}")
    void deleteDoctor(Integer doctorId);
    @Select("select username from doctors where doctor_id = #{doctorId}")
    String findByID(Integer doctorId);
    //更新医院信息
    @Update("update hospitals set hospital_name = #{hospitalName}, address = #{address} where hospital_id = #{hospitalId}")
    void updateHospital(Hospital hospital);
    //更新科室信息
    @Update("update outpatientclinics set clinic_name = #{clinicName}, clinic_description = #{clinicDescription} where clinic_id = #{clinicId}")
    void updateClinic(OutpatientClinic clinic);
    //更新部门信息
    @Update("update departments set department_name = #{departmentName}, contact_number = #{contactNumber} where department_id = #{departmentId}")
    void updateDepartment(Department department);
    //获取所有医生信息
    @Select("select * from doctors")
    List<Doctor> getAllDoctors();
}
